<?php
/**
Task:
C: Calculate and print 10 numbers for fibonacci series. Use recursion.

Extra: Create second algorithm for fibonacci but without recursion.


In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
F_n = F_{n-1} + F_{n-2},\!\,
with seed values[1][2]
F_1 = 1,\; F_2 = 1
or[4]
F_0 = 0,\; F_1 = 1.
*/
$first = 0;
$second = 1;
$max = 10;

// With recursion:
function fibonacciR($first, $second, $num)
{
    if ($num > 0) {
        $sum = $first + $second;
        $first = $second;
        $second = $sum;
        print($sum . "\n");
        fibonacciR($first, $second, $num - 1);
    }
}
fibonacciR($first, $second, $max);

// Without recursion:
function fibonacciNR($first, $second, $num)
{
    for ($i = 0; $i < $num; $i ++) {
        $sum = $first + $second;
        $first = $second;
        $second = $sum;
        echo $sum . "\n";
    }
}
fibonacciNR($first, $second, $max);

// Another method: http://en.wikipedia.org/wiki/Fibonacci_number#Computation_by_rounding
function fibonacciAlt($n)
{
    for ($i = 0; $i <= $n + 1; $i ++) {
        $value = round(pow((sqrt(5) + 1) / 2, $i) / sqrt(5));
        echo $value . "\n";
    }
}
fibonacciAlt($max);

